<?php

class LoginAction extends fvAction {
    
    function __construct () {
        parent::__construct(fvSite::$Layoult);
    }
    
    function executeIndex() {
        if (!fvRequest::getInstance()->isXmlHttpRequest()) {
            $this->current_page->setTitle('Login Page');
            $this->current_page->setTemplate('loginLayoult.tpl');
            return self::$FV_OK;
        } else {
            return self::$FV_AJAX_CALL;
        }
    }
    
    function executeDeny() {
        $this->current_page->setTitle('Deny Page');
        $this->current_page->setTemplate('loginLayoult.tpl');
        return self::$FV_OK;
    }
    
    function executeLogin() {
        $UserManager  = UserManager::getInstance();
        $request = fvRequest::getInstance();
        
        $login = $request->getRequestParameter("login");
        $password = $request->getRequestParameter('password');
        
        if (fvSite::$fvConfig->get('access.remote_login')) {

	    $client = new SoapClient("http://service.spo:4444/services.wsdl");

            $parameters = array(
                array(
                    'login' => $login,
                    'password' => md5($password),
                    'siteName' => fvSite::$fvConfig->get('server_name'),
                    'loginAcl' => 'acl_backend_login',
                )
            );
            
            try {
                $result = $client->__soapCall('doLOgin', $parameters);
                
                $LoggedUser = new User();
                if ($result['LoggedUser']->object_hash) {
                    $LoggedUser->hydrate(unserialize($result['LoggedUser']->object_hash));
                    //$User->save();
                } else {
                    $LoggedUser->setPk($result['LoggedUser']->id);
                    $LoggedUser->login = $result['LoggedUser']->login;
                    $LoggedUser->full_name = $result['LoggedUser']->full_name;
                    $LoggedUser->permitions = unserialize($result['LoggedUser']->permitions);
                    $LoggedUser->email = $result['LoggedUser']->email;
                }
            } catch (SoapFault $e) {
                /*var_dump('WEIFJWEFJ');
                $this->setFlash($e->getMessage(), self::$FLASH_ERROR);*/
            }
        } else {
            $LoggedUser = $UserManager->Login($login, $password);    
        }
        
        if ($LoggedUser instanceof User) {
            fvSite::$fvSession->setUser($LoggedUser);
            fvResponce::getInstance()->setHeader('loginsuccess', '1');
            if (fvSite::$fvSession->is_set("login/redirectURL"))
                $this->redirect("@#" . fvSite::$fvConfig->get('dir_web_root') . fvSite::$fvSession->get("login/redirectURL"));
            else $this->redirect("@");
        } else {
            $this->setFlash("Ошибка при аутентификации пользователя. Пара логин/пароль не найдена", self::$FLASH_ERROR);
            //$this->redirect(fvSite::$fvConfig->get('access.login_page'));
        }
        
        if (!fvRequest::getInstance()->isXmlHttpRequest()) {
            return self::$FV_OK;
        } else {
            return self::$FV_AJAX_CALL;
        }
    }

    function executeLogout() {
        fvSite::$fvSession->setUser(false);
        $this->redirect("");
    }
    
    function executeLoginform() {
        if (!fvRequest::getInstance()->isXmlHttpRequest()) {
            $this->redirect404();
        } else {
            return self::$FV_AJAX_CALL;
        } 
    }
}

?>
